package com.surveyingandmapping.ionospheretec.controller;

import com.surveyingandmapping.ionospheretec.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Iterator;

/**
 * Created by Carly rea jepsen on 2016/3/27.
 */
@Controller
public class FileController {

    @Autowired
    FileService fileService;

    @RequestMapping(value = "/upload" ,method = RequestMethod.POST)
    public Object readFile(HttpServletRequest request){
        try {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            if (multipartResolver.isMultipart(request)) {
                MultipartHttpServletRequest multipartRequset = (MultipartHttpServletRequest) request;
                Iterator<String> fileName = multipartRequset.getFileNames();
                while (fileName.hasNext()) {
                    MultipartFile file = multipartRequset.getFile(fileName.next());
                    if (file != null) {
                        File file1 = new File("C:/Users/Carly rea jepsen/Desktop/测绘工程/电离层解算/a.ipp");
                        file.transferTo(file1);

                        fileService.getObservationData(file1);
                    }
                }
                System.out.println();
            }
        }catch (Exception e)
        {
            return e.getMessage();
        }

        return "/success";
    }


}
